﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtDevice
{
    /// <summary>
    /// Single AT response.
    /// </summary>
    /// <example>
    /// <code>
    /// AtResponseItem item = new AtResponseItem("+CSQ: rssi,ber");
    /// int signalLevel = item.GetInt(0);
    /// </code>
    /// </example>
    public class AtResponseItem
    {
        string _command;
        string[] _data;
        string _fullData;
        string _pduData;

        /// <summary>
        /// An AT command as defined in 3GPP TS 27.007.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="extended">True if the response is non-standard command (eg. ^SYSINFO)</param>
        public AtResponseItem(string response)
            : this(response, false)
        {
        }

        /// <summary>
        /// An AT command as defined in 3GPP TS 27.007.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="extended">True if the response is non-standard command (eg. ^SYSINFO)</param>
        public AtResponseItem(string response, bool extended)
        {
            if(extended)
                ParseExtendedResponse(response);
            else
                ParseStandardResponse(response);
        }

        private void ParseExtendedResponse(string response)
        {
            _command = "";
            _fullData = response.Trim(new char[] { '^', ' ' });
            _data = response.Split(',');
        }

        private void ParseStandardResponse(string response)
        {
            string []parts = response.Split(':');
            _command = parts[0].Substring(1);
            _fullData = parts[1].Trim();
            _data = _fullData.Split(',');
        }

        /// <summary>
        /// Get nth integer value from the response.
        /// </summary>
        public int GetInt(int n)
        {
            return Int32.Parse(_data[n]);
        }

        /// <summary>
        /// Get nth string value from the response.
        /// </summary>
        public string GetString(int n)
        {
            return _data[n].Trim('"');
        }

        /// <value>
        /// Get the command part of the parsed line.
        /// </value>
        public string Command { get { return _command; } }

        /// <value>
        /// Get the data part of the parsed line.
        /// </value>
        public string Data { get { return _fullData; } }

        /// <summary>
        /// Get PDU data from the response (if any).
        /// </summary>
        public string GetPdu()
        {
            return _pduData;
        }

        public void SetPduData(string data)
        {
            _pduData = data;
        }

        public override string ToString()
        {
            return String.Join(" ", _data);
        }
    }

    public class AtResponse
    {
        private string _plainText;
        private List<AtResponseItem> _items = new List<AtResponseItem>();

        public List<AtResponseItem> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public void SetPlainText(string response)
        {
            _plainText = response;
        }

        public void AddItem(AtResponseItem item)
        {
            _items.Add(item);
        }

        public int GetInt(int n)
        {
            return _items[0].GetInt(n);
        }

        public string GetString(int n)
        {
            return _items[0].GetString(n);
        }

        /// <summary>
        /// Gets the plain text response if any.
        /// </summary>
        /// <returns></returns>
        public string GetPlainText()
        {
            return _plainText;
        }
    }
}
